package com.lcxw.hxzbadmin.controller.AnchorLevelController;

import com.alibaba.fastjson.JSONObject;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.pojo.AnchorLevel;
import com.lcxw.hxzbadmin.entity.requestbean.AnchorLevelRequestBean;
import com.lcxw.hxzbadmin.service.baseservice.AnchorService;
import com.lcxw.hxzbapi.entity.response.Response;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AnchorLevelController {

    private Logger logger = LoggerFactory.getLogger(AnchorLevelController.class);

    @Resource
    AnchorService anchorService;

    @PostMapping("/anchorLevel/add")
    public Response addAnchorLevel(@RequestBody AnchorLevelRequestBean anchorLevelRequestBean) {
        logger.info("admin {} add anchor levels:{}.", anchorLevelRequestBean.getAdminName(),
            anchorLevelRequestBean.getAnchorLevels());

        List<AnchorLevel> anchorLevelList = anchorLevelRequestBean.getAnchorLevels();

        for (AnchorLevel anchorLevel : anchorLevelList
        ) {
            try {
                anchorService.insert(anchorLevel);
            } catch (Exception e) {
                return Response.dbFail(logger, "insert anchorLevel ");
            }

        }

        return Response.common(logger, "insert anchorLevel success", 0, "success", null);

    }

    @PostMapping("/anchorLevel/mod")
    public Response modAnchorLevel(@RequestBody AnchorLevelRequestBean anchorLevelRequestBean) {
        logger.info("admin {} mod anchor levels:{}.", anchorLevelRequestBean.getAdminName(),
            anchorLevelRequestBean.getAnchorLevels());
        List<AnchorLevel> anchorLevelList = anchorLevelRequestBean.getAnchorLevels();

        for (AnchorLevel anchorLevel : anchorLevelList
        ) {
            try {
                int levelId = anchorLevel.getLevelId();
                if (null == anchorService.selectByPrimaryKey(levelId)) {
                    anchorService.insert(anchorLevel);
                } else {

                    anchorService.updateByPrimaryKey(anchorLevel);
                }
            } catch (Exception e) {
                return Response.dbFail(logger, "insert anchorLevel ");
            }

        }

        return Response.common(logger, "mod anchorLevel success", 0, "success", null);
    }

    @PostMapping("/anchorLevel/getAll")
    public Response getAllAnchorLevel(@RequestBody JSONObject requestParams) {
        logger.info("admin {} get all anchor levels.", requestParams.getString("adminName"));
        List<AnchorLevel> anchorLevelList;
        try {
            anchorLevelList = anchorService.selectAll();
        } catch (Exception e) {
            return Response.dbFail(logger, "get all anchor level");
        }

        Map<String, Object> data = new LinkedHashMap<>();

        data.put("count", anchorLevelList.size());
        data.put("anchorLevelList", anchorLevelList);

        return Response.common(logger, "insert anchorLevel success", 0, "success", data);
    }
}